<?php

class Default_ArticleGalleryController extends Zend_Controller_Action
{

    public function init()
    {
        Zend_Session::start();
        $this->Session = new Zend_Session_Namespace ('Params');
        $this->view->Persisted   = $this->Session->Persisted; 
    }

    public function viewAction()
    {
        // action body
        $this->_helper->layout->setLayout('blank');
        $request    = $this->getRequest(); 
        $groupname  = $request->getParam('group'); 
        $pagenumber = $request->getParam('page'); 
        $article    = $request->getParam('article'); 
        $index      = $request->getParam('index');  
          
        $g = $this->view->ArticleList($groupname);
        $articles = $g->GetArticleSet($article, true);
        $articles->CheckPreview($groupname);

        $this->view->subject = $articles->subject; 
        if (!$articles->media) {
            $selected = $articles->FindById($index); 
            $b = new Default_Model_BookmarkManager;
            $bookmarked = $b->Find($groupname, $articles->id);
        }

        $this->view->group       = $groupname;
        $this->view->page        = $pagenumber;
        $this->view->articleset  = $articles;
        $this->view->article     = $article;
        $this->view->index       = $index;
        $this->view->marked      = $bookmarked;  
        $this->view->articles    = $articles->items;  
        if ($selected) $this->view->subject  = $selected->subject;
    }

    public function browseAction()
    { 
        $request    = $this->getRequest(); 
        $groupname  = $request->getParam('group'); 
        $pagenumber = $request->getParam('page'); 
        $article    = $request->getParam('article'); 
        $index      = $request->getParam('index');  
        $show       = $request->getParam('show');  

        $targetURL  = "/article-gallery/{$groupname}/{$pagenumber}/{$article}/{$index}/";



            $g = $this->view->ArticleList($groupname);
            $articles = $g->GetArticleSet($article, true);  
        $selected = $articles->FindById($index);
            $startIndex = ($show - 1) * 8;  
        $this->view->group    = $groupname;
            $this->view->articles = array_slice ($articles->items, $startIndex, 8);

        $b = new Default_Model_BookmarkManager;
        $bookmarked = $b->Find($groupname, $articles->id);

        $this->view->group       = $groupname;
        $this->view->page        = $pagenumber;
        $this->view->marked      = $bookmarked; 
        $this->view->prefix      = $targetURL; 
        $this->view->article     = $article; 
        $this->view->articlepage = $show; 
        $this->view->articlesize = sizeof($articles->items); 
        if ($selected) $this->view->subject  = $selected->subject;

    }

    public function indexAction()
    { # /article-gallery/:groupname/:pagenumber/:article/:index/:show
        $request    = $this->getRequest(); 
        $groupname  = $request->getParam('groupname'); 
        $pagenumber = $request->getParam('pagenumber'); # list PAGENUMBER
        $article    = $request->getParam('article');    # article INDEX 
        $index      = $request->getParam('index');      # article ID
        $show       = $request->getParam('show');       # browse PAGENUMBER


        $base64Name       = base64_encode ($index);
        $picturePath      = DATA_PATH . "/asset/{$groupname}/{$base64Name}"; 

            $data = array ('groupname'  => $groupname,
                            'endpoint'   => ARTICLE_CLIENT, 
                            'article'    => $index );

        $this->_helper->layout->disableLayout();
        if (is_numeric($show)) {

            $g = $this->view->ArticleList($groupname);
            $articles = $g->GetArticleSet($article, true);
            if (!$articles) {
                 echo "ERROR: Could not locate article list ({$article})<xmp>";
                 var_dump($request);
                 exit;
            }  
            $startIndex = ($show - 1) * 8;  
            $picsonpage = array_slice ($articles->items, $startIndex, 8);

            $key = array();
            foreach ($picsonpage as $pic) $key[] = $pic->id;

            $data = array ('groupname'  => $groupname,
                            'endpoint'   => ARTICLE_CLIENT, 
                            'article'    => implode('/', $key) );
            $targetURL  = "/article-gallery/browse/group/{$groupname}/page/{$pagenumber}/article/{$article}/index/{$index}/show/{$show}"; 

        } else if (file_exists($picturePath)) {
        	echo "OK";
			return;
        } 
        
        
        $MSGKEY = 519059;  # TO DO: Config 
        $Queue  = new  Default_Model_QueueManager($MSGKEY, $data); 
        $Id     = $Queue->SendMessage(); 
  
        echo $Id;
        return; 
    } 
}

